package com.vecspace.data.numerical;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.vecspace.utils.NumberUtil;

@JsonSerialize(using = HNumericalJsonSerializer.class)  
@JsonDeserialize(using = HPercentJsonDeserializer.class) 
public class HPercent extends HNumerical{
	private static final long serialVersionUID = -8397213886295875001L;

	int decimalDigits;//百分化后保留的小数位数

	public HPercent() {
		super();
	}
	/**
	 * 
	 * @param value
	 * @param decimalDigits 百分化后保留的小数位数
	 */
	public HPercent(BigDecimal value, int decimalDigits) {		
		this.decimalDigits = decimalDigits;
		this.setValue(value);
	}

	public HPercent(BigDecimal value) {		
		this.decimalDigits = 2;
		this.setValue(value);
	}

	public int getDecimalDigits() {
		return decimalDigits;
	}

	public void setDecimalDigits(int decimalDigits) {
		this.decimalDigits = decimalDigits;
	}

	@Override
	public String getText() {
		if(this.getValue() == null)return "";
		String text = formatPercentage(this.getValue(), this.decimalDigits);
		return text;
	}

	@Override
	public void setText(String text) {
		BigDecimal num = this.parseDouble(text);
		if(text != null) {
			if(!text.trim().endsWith("%") && num.doubleValue() > 0.999999) {
				num = NumberUtil.divide(num , BigDecimal.valueOf(100.0), 4);
			}else if(text.trim().endsWith("%")){
				num = NumberUtil.divide(num , BigDecimal.valueOf(100.0), 4);
			}
		}
		this.setValue(num);
	}
	
	/**返回百分比的分子（含%号）
	 * 
	 * @param value
	 * @param decimalDigits
	 * @return 
	 * @author: wanghua
	 */
	public static String formatPercentage(BigDecimal value, int decimalDigits) {
		if(value == null)return "";
		BigDecimal percent = NumberUtil.multiply(value , BigDecimal.valueOf(100.0));
		DecimalFormat df = null;
		if(0 == decimalDigits) {//没有小数
			df = new DecimalFormat("#0");
		}else if(1 == decimalDigits) {//1位小数
			df = new DecimalFormat("#0.0");
		}else if(2 == decimalDigits) {//1位小数
			df = new DecimalFormat("#0.0#");
		}else if(3 == decimalDigits) {//1位小数
			df = new DecimalFormat("#0.0##");
		}
		
		df.setRoundingMode(RoundingMode.HALF_UP);
		   
        String fmtValueStr = df.format(percent);
        return fmtValueStr+"%";
	}

	@Override
	public Class<HPercent> returnedClass() {
		return HPercent.class;
	}

	@Override
	public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
			throws HibernateException, SQLException {
		BigDecimal value = rs.getBigDecimal(names[0]);
		HPercent obj = new HPercent(value, 2);
		return obj;
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		if(value == null)return null;
		HPercent obj1 = (HPercent)value;
		HPercent obj2 = new HPercent(obj1.getValue(), obj1.decimalDigits);
		obj2.setSeparatorDigit(obj1.getSeparatorDigit());
		return obj2;
	}
}
